package com.example.jiangqiangwei.uselibpermission;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.zhy.m.permission.MPermissions;
import com.zhy.m.permission.PermissionDenied;
import com.zhy.m.permission.PermissionGrant;

/**
 * GitHub上的MPermission库的使用
 */
public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_CODE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onclick(View view) {
        //获取sdcard权限,调用工具里面的申请权限
        MPermissions.requestPermissions(this, REQUEST_CODE, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        MPermissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults);

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @PermissionGrant(REQUEST_CODE)
    public void success() {
        Toast.makeText(this, "获得权限之后的操作", Toast.LENGTH_SHORT).show();
    }

    @PermissionDenied(REQUEST_CODE)
    public void fail() {
        Toast.makeText(this, "获得权限失败", Toast.LENGTH_SHORT).show();
    }
}
